var code = "";
/******验证码生成函数********/
function validation() {
var code_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P',
		'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
		'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
	],
	color_list = ['#e3730f', '#2ae3e0', '#c317e3', '#0e935f', '#e30d65', '#3c8b36', '#74675a', '#654321', '#123345',
		'#135784', '#169b9a', '#02468a'
	],
	bgcolor_list = ['#b1b1b1', '#e3d7cc', '#d0d9f9', '#f8f2d6'],
	_code = "",
	code_color = "",
	code_bgcolor = "",
	code_length = 6,
	deg = 0,
	html = "";
	for (var i = 0; i < code_length; i++) {
		var index = Math.floor(Math.random() * code_list.length),
			idx = Math.floor(Math.random() * color_list.length),
			bgIdx = Math.floor(Math.random() * bgcolor_list.length);
		if (index % 2 == 0)
			deg = index + Math.floor(Math.random() * idx);
		else
			deg = -(index + Math.floor(Math.random() * idx));
		html +=
			`<span style="padding:0 2px; color:${color_list[idx]};display:inline-block;transform:rotate(${deg}deg)">${code_list[index]}</span>`;
		_code += code_list[index];
		code = _code;
		code_bgcolor = bgcolor_list[bgIdx];
	}
	$("#code").css({
		width:code_length * 20 + "px",
		fontSize: "20px",
		backgroundColor: code_bgcolor,
		textAlign: "center",
		padding: "3px",
		marginLeft: "3px",
		opacity: .6,
		userSelect: "none",
		display: "inline-block"
	})
	$("#code").empty().append(html);
}

//隐藏
$("#registe").click(function () {
	$(".zs-r").addClass("lag").removeClass("ppt");
	$(".zs-l").removeClass("lag").addClass("ppt");
	$("input").val("");
})
$("#fanhui").click(function () {
	$(".zs-l").addClass("lag").removeClass("ppt");
	$(".zs-r").removeClass("lag").addClass("ppt");
	$("input").val("");
})
/****调用生成验证码函数*****/
validation();
/********点击更换验证码*********/
$("#code").click(function () {
	validation();
})




/*******登录验证********/
$("#login").click(function () {
	var phone = $("#log_phone");
	var pwd = $("#log_pwd");
	if (!phone.val())
		phone.parent().next().children().html("手机号不能为空");
	if (!pwd.val())
		pwd.parent().next().children().html("密码不能为空");
	if (pwd.val() && phone.val()) {
		$.ajax({
			type: "post",
			url: "/login/log",
			async: true,
			data: {
				phone: phone.val(),
				pwd: pwd.val()
			},
			success: function (data) {
				console.log(data)
				if (data.code == 200) {
					$.cookie("uid", data.uid);
					$(location).prop('href', 'index.html');
				} else {
					alert("账号或密码错误");
				}
			}
		});
	}
})

$("#log_phone").focus(function () {
	$("span").html("");
})
$("#log_pwd").focus(function () {
	$("span").html("");
})




/******注册验证*******/
$("#enroll_phone").blur(function () {
	var phone = $("#enroll_phone");
	var reg = /^1[34578]\d{9}$/;
	if (!phone.val())
		phone.next().html("手机号不能为空")
	else if (!reg.test(phone.val()))
		phone.next().html("请输入正确的手机号")
	else {
		$.ajax({
			type: "get",
			url: "/user/verification",
			async: true,
			data: {
				phone: phone.val()
			},
			success: function (data) {
				if (data.code == 301)
					phone.next().html("该号码已注册")
				if (data.code == 200)
					phone.next().html("")
			}
		});
	}
})

$("#enroll_code").blur(function(){
	$("#enroll_code").next().next().html("");
})

$("#enroll_pwd").blur(function () {
	var pwd = $("#enroll_pwd");
	var reg = /^[a-z0-9_-]{6,16}$/;
	if (!pwd.val())
		pwd.next().html("密码不能为空");
	else if (!reg.test(pwd.val()))
		pwd.next().html("密码格式不正确");
	else
		pwd.next().html("")
})


$("#re_pwd").blur(function () {
	if ($(this).val() != $("#enroll_pwd").val())
		$(this).next().html("两次输入密码不一致")
	else {
		$(this).next().html("")
	}
})

/**********点击注册**********/
$("#enroll").click(function () {
	var enroll_code = $("#enroll_code").val();
	code = code.toUpperCase();
	enroll_code = enroll_code.toUpperCase();
	if (!$("#enroll_phone").val())
		$("#enroll_phone").next().html("手机号不能为空");
	else if (!$("#enroll_pwd").val())
		$("#enroll_pwd").next().html("密码不能为空");
	else if (!/^1[34578]\d{9}$/.test($("#enroll_phone").val()))
		$("#enroll_phone").next().html("请输入正确的手机号");
	else if (!/^[a-z0-9_-]{6,16}$/.test($("#enroll_pwd").val()))
		$("#enroll_pwd").next().html("密码格式不正确");
	else if ($("#re_pwd").val() != $("#enroll_pwd").val())
		$("#re_pwd").next().html("两次输入密码不一致");
	else if (enroll_code != code)
		$("#enroll_code").next().next().html("验证码错误")
	else {
		$.ajax({
			type: "get",
			url: "/user/verification",
			async: true,
			data: {
				phone: $("#enroll_phone").val()
			},
			success: function (data) {
				if (data.code == 301)
					$("#enroll_phone").next().html("该号码已注册")
				else if (data.code == 200) {
					$.ajax({
						type: "post",
						url: "/user/add",
						async: true,
						data: {
							phone: $("#enroll_phone").val(),
							pwd: $("#re_pwd").val()
						},
						success: function (result) {
							/******注册成功后的操作******/
						}
					})
				}
			}
		})
	}
})
